<?php


namespace app\mobile\controller;

use think\Request;
use think\Db;

class Aucing extends \Common1 {

    public $token = "";

    public function __construct(Request $request = null){
        parent::__construct();
        if(\request()->action() == "postmybid"){
            $arr = getallheaders();
            $token = $this->Check(isset($arr["Authorization"]) ? $arr["Authorization"] : "");
            $this->token = $token;
        }
    }

    // 查看当前拍卖的
    public function CurrGoods(){
        if(isset($_GET["id"])){

            $good = Db::name("goods")->where("lid", $_GET["id"])->where("state", 1)->find();
            if(empty($good)){
                $good = Db::name("goods")->where("lid", $_GET["id"])->where("state", 3)->order("clinch_time desc")->find();
            }
            $data = false;
            if(!empty($good)){
                $lot = Db::name("lot")->where("lid", $_GET["id"])->find();
                $image = $this->SearchImg($lot["lid"], $good["number"]);

                $buy = 0;
                if($good["nid"] != 0){
                    $n = Db::name("num")->where("nid", $good["nid"])->find();
                    $buy = $n["number"];
                }else if($good["up_id"] != 0){
                    $admin = Db::name("user_proxy")->where("up_id", $good["up_id"])->find();
                    $buy = $admin["name"];
                }

                if($good["state"] == 1){
                    $final = "";
                }else{
                    $final = $good["final_price"] == 0 ? "流拍" : $good["final_price"];
                }

                $eval = eval_price($good, 2);

                $data = [
                    "id" => (int)$good["gid"],
                    "subid" => $lot["aid"],
                    "cover" => $image[0],
                    "curr" => $good["final_price"] == 0 ? $good["price"] : $good["final_price"],
                    "curr_buyer" => $good["final_price"] == 0 ? "" : $buy,
                    "curr_convert" => $good["final_price"] == 0 ? "￥ ".number_format((int)$good["price"] * (float)$lot["rate"],0) : "￥ ".number_format((int)$good["final_price"] * (float)$lot["rate"], 0),
                    "final" => $final,
                    "lot" => $good["number"],
                    "name" => $good["name"],
                    "start" => $good["price"],
                    "start_convert" => "￥ ".number_format((int)$good["price"] * (float)$lot["rate"], 0),
                    "start_unit" => "￥",
                    "state" => (int)$good["state"],
                    "eval" => $eval,
                    "livePlay" => $this->checkIsPush("lot{$good["lid"]}")
                ];
            }
            Code(200, false, $data);
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }


    // 用户出价
    public function PostMyBid(){
        $ub_id = $this->token["id"];
        if(isset($_POST["id"]) && isset($_POST["bid"])){
            $gid = $_POST["id"]; $price = $_POST["bid"];

            $user = Db::name("user_buy")->where("ub_id", $ub_id)->find();
            $good = Db::name("goods")->where("gid", $gid)->find();
            $lot = Db::name("lot")->where("lid", $good["lid"])->find();
            $pay_com = Db::name("user_buy_company")->where("aid", $lot["aid"])->where("ub_id", $ub_id)->find();
            $number = Db::name("num")->where("aid", $lot["aid"])->where("ub_id", $ub_id)->find();
            if(!empty($number)){
                if($pay_com["pay_state"] != 0){
                    if($good["state"] == 1){
                        if(($good["final_price"] == 0 && $price >= $good["price"]) || ($good["final_price"] !=0 && $price > $good["final_price"])){
                            if($good["final_price"] == 0){
                                $revoke = "";
                            }else{
                                $admin = Db::name("user_proxy")->where("up_id", $good["up_id"])->find();
                                $revoke = [
                                    "num" => $good["up_id"] == 0 ? $good["nid"] : $admin["name"],
                                    "bid" => $good["final_price"]
                                ];
                            }
                            $send = [
                                "type" => "bidnet",
                                "lot_id" => $good["lid"],
                                "num" => $number["number"],
                                "bid" => $price
                            ];
                            $this->sendTogrounp($send, $good["lid"]);
                            Code(200, "已提交，等待出价审核", (object)[]);
                        }else{
                            Code(403, "出价必须高于当前价");
                        }
                    }else{
                        Code(403, "拍品未开拍");
                    }
                }else{
                    Code(403, "您不是该代理下的vip，请前去交保证金");
                }
            }else{
                Code(403, "您未绑定号码牌，无法出价");
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

}